/*
   @Copyright:LeetCode
   @Author:   tjyemail
   @Problem:  http://leetcode.com/problems/balanced-binary-tree
   @Language: C++
   @Datetime: 19-07-16 19:41
   */

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
	int depth(TreeNode *root){
		if(root==NULL) return 0;
		int left = depth(root->left);
		if(left==-1) return -1;
		int right = depth(root->right);
		if(right==-1) return -1;
		return abs(left-right)<2?max(left,right)+1:-1;
	}
public:
	bool isBalanced(TreeNode* root) {
		return depth(root)!=-1;
	}
};
